package com.Service.Impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.Dao.BaseDaoI;
import com.Entity.DataInfo;
import com.Entity.UsersInfo;
import com.Service.UserService;

@Service("userService")
public class UserServiceImpl extends BaseServiceImpl<UsersInfo> implements
		UserService {

	List<UsersInfo> userlist = new ArrayList<UsersInfo>();
	List<DataInfo> datalist = new ArrayList<DataInfo>();
	List<DataInfo> datalist1 = new ArrayList<DataInfo>();
	UsersInfo user = new UsersInfo();
	DataInfo data = new DataInfo();

	@Autowired
	private BaseDaoI<UsersInfo> usersdao;

	// login
	@Override
	public List<UsersInfo> checkUser(String login, String password) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("login", login);
		params.put("password", password);
		String hql = "from UsersInfo t where t.cellphone = :login and t.password = :password";
		userlist = this.usersdao.find(hql, params);
		if (userlist.isEmpty()) {
			String hql1 = "from UsersInfo t where t.userName = :login and t.password = :password";
			this.userlist = this.usersdao.find(hql1, params);
		}
		return userlist;
	}

	// register
	@Override
	public int findByUserName(String username) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userName", username);
		String hql = "from UsersInfo t where t.userName = :userName";
		userlist = this.usersdao.find(hql, params);
		if (!userlist.isEmpty() && userlist.get(0).getUserId() > 0) {
			return userlist.get(0).getUserId();
		} else {
			return 0;
		}
	}

	public List<UsersInfo> checkLogin(String phoneNum, String password) {

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("phoneNum", phoneNum);
		params.put("password", password);
		String hql = "from UsersInfo t where t.cellphone = :phoneNum and t.password = :password";
		userlist = this.usersdao.find(hql, params);
		return userlist;

	}

	// 查询用户存不存在
	public List<UsersInfo> findByPhoneNum(String phoneNum) {

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("phoneNum", phoneNum);

		String hql = "from UsersInfo t where t.cellphone = :phoneNum";
		userlist = this.usersdao.find(hql, params);

		if (null != userlist && !userlist.isEmpty()) {
			return userlist;
		} else {
			return null;
		}

	}

	@Override
	public List<UsersInfo> getSearchList(String search) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("search", search);

		String hql = "from UsersInfo t where t.cellphone = :search";
		userlist = this.usersdao.find(hql, params);

		if (null != userlist && !userlist.isEmpty()) {
			return userlist;
		} else {
			String hql1 = "from UsersInfo t where t.userName = :search";
			this.userlist = this.usersdao.find(hql1, params);

			if (null != userlist && !userlist.isEmpty()) {
				return userlist;
			} else {
				return null;
			}
		}
	}

	@Override
	public List<UsersInfo> findAll() {
		// TODO Auto-generated method stub
		String hql = "from UsersInfo";
		userlist = this.usersdao.find(hql);
		return userlist;
	}

}
